/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Dao;

import Entity.ketqua;
import Util.ConSQL;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComboBox;

/**
 *
 * @author AnhPhuong
 */
public class ketquaDao {
     ConSQL cs = new ConSQL();
     ketqua kqe = new ketqua();
  public void cbbLopHocPhan(JComboBox cbb) throws Exception{
        cbb.removeAllItems();
        Statement st = cs.getCon().createStatement();
        ResultSet rs = st.executeQuery("select tenLopHP from lopHP");
        
        while(rs.next()){
            cbb.addItem(rs.getString(1));
        }
    }
  public void cbbmonHoc(JComboBox cbb) throws Exception{
        cbb.removeAllItems();
        Statement st = cs.getCon().createStatement();
        ResultSet rs = st.executeQuery("select maMH from monHoc");
        
        while(rs.next()){
            cbb.addItem(rs.getString(1));
        }
    }
  public void cbbsinhvien(JComboBox cbb) throws Exception{
        cbb.removeAllItems();
        Statement st = cs.getCon().createStatement();
        ResultSet rs = st.executeQuery("select maSV from lopHP_SV");
        
        while(rs.next()){
            cbb.addItem(rs.getString(1));
        }
    }
  public ArrayList<ketqua> selectAll() {
        ketqua kq;
        ArrayList<ketqua> list = new ArrayList<ketqua>();
        try {
            String sql = "select * from ketQua";
            CallableStatement cs = ConSQL.getCon().prepareCall(sql);
            ResultSet rs = cs.executeQuery();
            while (rs.next()) {
                kq = new ketqua();
                kq.setMaSV(rs.getString("maMH"));
                kq.setMaMH(rs.getString("maSV"));
                kq.setMaMH(rs.getString("hocKi"));
                kq.setDChuyencan(rs.getFloat("DChuyencan"));
                kq.setDGiuaky(rs.getFloat("DGiuaky"));
                kq.setDThiKetthuc(rs.getFloat("DThiKetthuc"));
                kq.setDTrungBinh(rs.getFloat("DTrungBinh"));
                kq.setXepLoai(rs.getString("xepLoai"));
                kq.setDTichLuy(rs.getFloat("DTichLuy"));
                kq.setGhiChu(rs.getString("ghiChu"));
                list.add(kq);
            }
        } catch (SQLException ex) {
            Logger.getLogger(ketquaDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return list;
    }
  public boolean nhapdiem(ketqua kqe){
      try {
            String sql = "insert into ketQua values (?,?,?,?,?,?,?,?,?,?)";
            CallableStatement pst = ConSQL.getCon().prepareCall(sql);
            pst.setString(1, kqe.getMaMH());
            pst.setString(2, kqe.getMaSV());
            pst.setInt(3, kqe.getHocKy());
            pst.setFloat(4, kqe.getDChuyencan());
            pst.setFloat(5, kqe.getDGiuaky());
            pst.setFloat(6, kqe.getDThiKetthuc());
            pst.setFloat(7, kqe.getDTrungBinh());
            pst.setString(8, kqe.getXepLoai());
            pst.setFloat(9, kqe.getDTichLuy());
            pst.setString(10, kqe.getGhiChu());
            return (pst.executeUpdate() == 1);
        } catch (SQLException ex) {
            Logger.getLogger(ketquaDao.class.getName()).log(Level.SEVERE, null, ex);
            return  false;
        }
  }
  public boolean update(ketqua kqe) {
        try {
            PreparedStatement ps = ConSQL.getCon().prepareStatement("update ketQua set  hocKy=?,"
                    + "DChuyencan=?,DGiuaky=?, DThiKetthuc=?, DTrungBinh=?, xepLoai=?,"
                    + "DTichLuy=?, ghiChu=? where maSV=? and maMH = ? ");

            ps.setInt(1,kqe.getHocKy());
            ps.setFloat(2,kqe.getDChuyencan() );
            ps.setFloat(3, kqe.getDGiuaky());
            ps.setFloat(4, kqe.getDThiKetthuc());
            ps.setFloat(5, kqe.getDTrungBinh());
            ps.setString(6, kqe.getXepLoai());
            ps.setFloat(7, kqe.getDTichLuy());
            ps.setString(8, kqe.getGhiChu());
            ps.setString(9, kqe.getMaSV());
            ps.setString(10, kqe.getMaMH());
            return (ps.executeUpdate() == 1);
        } catch (SQLException ex) {
            Logger.getLogger(ketquaDao.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }
}
